package com.qianmi.cloud.config;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;

/**
 * @Description
 * @Date: 1/20/22 10:28
 * @Created by xiaocai
 */
@Order(3)
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerAdapterConfigurer extends ResourceServerConfigurerAdapter {

    @Autowired
    private DefaultTokenServices tokenServices;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.tokenServices(tokenServices);
    }

    /**
     * 权限忽略控制
     *
     * @param http
     */
    @SneakyThrows
    @Override
    public void configure(HttpSecurity http) {
        String[] allIgnoredPath = {"/oauth/**", "/login"};
        http.authorizeRequests().antMatchers(allIgnoredPath)
                .permitAll()
                .anyRequest().authenticated().and().csrf().disable();
    }
}
